package com.base.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeEventManager {
	/**
	 * 事件列表
	 */
	private List<TreeEvent> events = new ArrayList<TreeEvent>();
	
	/**
	 * 注册事件
	 * @param event 事件对象
	 */
	public void registerEvent(TreeEvent event){
		if(event == null) return;
		if(!this.events.contains(event)) {
			TreeEvent sameTypeEvent = null;
			for(TreeEvent treeEvent : this.events) {
				if(event.getType() == treeEvent.getType()) {
					sameTypeEvent = treeEvent;
				}
			}
			this.removeEvent(sameTypeEvent);
			this.events.add(event);
		}
	}
	
	/**
	 * 删除事件
	 * @param eventID 事件标示
	 * @return 已删除的事件
	 */
	public TreeEvent removeEvent(String eventID) {
		TreeEvent event = null;
		for(TreeEvent TreeEvent : this.events) {
			if(TreeEvent.getId().equals(eventID)) {
				event = TreeEvent;
				this.events.remove(TreeEvent);
				break;
			}
		}
		return event;
	}
	
	/**
	 * 删除事件
	 * @param event 需删除的事件
	 * @return 是否删除成功
	 */
	public boolean removeEvent(TreeEvent event) {
		boolean flag = false;
		if(event == null) return flag;
		
		flag = (null != this.removeEvent(event.getId()));
		
		return flag;
	}
	
	/**
	 * 删除所有事件
	 */
	public void removeAllEvents() {
		this.events.clear();
	}
	
	/**
	 * 获取事件列表
	 * @return 事件列表
	 */
	public TreeEvent[] getEvents() {
		return events.toArray(new TreeEvent[0]);
	}
}
